//
//  QKCamerViewController.m
//  Unity-iPhone
//
//  Created by 齐凯 on 2018/5/28.
//

#import "QKCamerViewController.h"



@implementation QKCamerViewController

static QKCamerViewController *qkCamer;

+ (QKCamerViewController *)instant{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        qkCamer = [[QKCamerViewController alloc] init];
        UIViewController *vc = UnityGetGLViewController();
        [vc.view addSubview:qkCamer.view];
        
    });
    return qkCamer;
}

#if defined(__cplusplus)
extern"C"{
#endif
    void  IOS_OpenCamer(){
        [QKCamerViewController instant];
        [qkCamer showPicker:(UIImagePickerControllerSourceTypeCamera) allowsEditing:YES];
    }
    void IOS_OpenAlbum(){
        [QKCamerViewController instant];
        [qkCamer showPicker:(UIImagePickerControllerSourceTypePhotoLibrary) allowsEditing:YES];
    }
    void IOS_OpenPhoto(){
        [QKCamerViewController instant];
        [qkCamer showPicker:(UIImagePickerControllerSourceTypeSavedPhotosAlbum) allowsEditing:YES];
    }
    
    
#if defined(__cplusplus)
}
#endif


- (void)showPicker:
(UIImagePickerControllerSourceType)type
     allowsEditing:(BOOL)flag
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = type;
    picker.allowsEditing = flag;
    [self presentViewController:picker animated:YES completion:nil];
    
}
#pragma mark - 代理事件
//4 选择完图片的操作
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //通过key值获取到图片   UIImagePickerControllerEditedImage (裁剪之后的图片)
    //未经裁剪的图片   UIImagePickerControllerOriginalImage
    UIImage * image =info[UIImagePickerControllerEditedImage];    //
    //将照片存到媒体库  (如果是拍照就保存)
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        UIImageWriteToSavedPhotosAlbum(image, self,@selector(image:didFinishSavingWithError:contextInfo:), NULL);
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
    //系统压缩
    NSData *imageData =  [self compressionSystem:image];
    //    NSLog(@" imageDate ====%@",imageData);
    [self saveimage:imageData];
}
#pragma mark - 照片存本地 生成本地路径
- (void)saveimage:(NSData *)imageData {
    
    NSString *str = [imageData base64EncodedStringWithOptions:(NSDataBase64Encoding64CharacterLineLength)];
    UnitySendMessage("RawImage", "camerbackStr", str.UTF8String);
}

#pragma mark - 照片存到本地后的回调 5
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{
    if (!error) {
        NSLog(@"存储成功");
    } else {
        //NSLog(@"存储失败：%@", error);
        //[SVProgressHUD showErrorWithStatus:@"存储失败"];
    }
}
#pragma mark - 取消操作时调用  7
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}
#pragma mark 获取压缩图片
- (NSData *)Imagecompression:(UIImage *)image scaleToSize:(CGSize)scaleToSize{
    UIImage *imageScal = [self OriginImage:image scaleToSize:scaleToSize];
    NSData *imageData =  [self compressionSystem:imageScal];
    return imageData;
}
#pragma mark - 比例压缩
/**
 *  压缩图片
 *  image:将要压缩的图片   size：压缩后的尺寸
 *  总结：关键在于获取屏幕密度：[UIScreen mainScreen].scale
 */
-(UIImage *) OriginImage:(UIImage *)image scaleToSize:(CGSize)size
{
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return scaledImage;   //返回的就是已经改变的图片
}

- (NSData *)compressionSystem:(UIImage *)image{
    //图片压缩
    //设置照片的品质 系统自带的方法，照片的像素和大小压缩有一定的限制
    NSData *imageData = UIImageJPEGRepresentation(image, 1);
    if (imageData.length>100*1024)
    {
        if (imageData.length>1024*1024) {//1M以及以上
            imageData = UIImageJPEGRepresentation(image, 0.1);
        }else if (imageData.length>512*1024) {//0.5M-1M
            imageData = UIImageJPEGRepresentation(image, 0.5);
        }else if (imageData.length>200*1024) {//0.25M-0.5M
            imageData = UIImageJPEGRepresentation(image, 0.9);
        }
    }
    return imageData;
}









@end









